package com.kongwg.preparedStatement.crud;

import com.kongwg.util.JDBCUtil;
import org.junit.Test;

import java.io.IOException;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;

/**
 * @author kongwg
 * @create 2021-01-07 16:25
 */
public class preparedStatementUptateTest {

    @Test
    public void testInsert() throws SQLException, IOException, ClassNotFoundException, ParseException {
        //获取链接
        Connection conn = JDBCUtil.getConnetion();

        //
        String sql = "INSERT INTO customers(name,email,birth) VALUES(?,?,?)";
        PreparedStatement ps = conn.prepareStatement(sql);

        ps.setString(1, "哪吒");
        ps.setString(2, "nezha@gamil.com");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        java.util.Date date = sdf.parse("1000-01-01");
        System.out.println(new Date(date.getTime()));
        ps.setString(3, String.valueOf(new Date(date.getTime())));

        ps.execute();
        JDBCUtil.closeResource(conn, ps);
    }

    @Test
    public void testDelete() {
        Connection conn = null;
        PreparedStatement ps = null;
        try {
            conn = JDBCUtil.getConnetion();
            String sql = "delete from customers where id = ?";
            ps = conn.prepareStatement(sql);
            ps.setString(1, "19");
            ps.execute();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCUtil.closeResource(conn, ps);
        }


    }

    @Test
    public void testUpdate() throws Exception {
        Connection conn = null;
        PreparedStatement ps = null;
        try {
            conn = JDBCUtil.getConnetion();
            String sql = "update customers set name = ? where id = ?";
            ps = conn.prepareStatement(sql);
            ps.setObject(1, "莫扎特");
            ps.setObject(2, 18);

            ps.execute();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCUtil.closeResource(conn, ps);
        }

    }


    /**
     * 通用增删改
     */

    public void update(String sql, Object... args){
        Connection conn = null;
        PreparedStatement ps = null;
        try {
            conn = JDBCUtil.getConnetion();
            ps = conn.prepareStatement(sql);

            for (int i = 0; i < args.length; i++) {
                ps.setObject(i + 1, args[i]);
            }
            ps.execute();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCUtil.closeResource(conn, ps);
        }

    }

    @Test
    public void  test1(){
//        String sql = "delete from `order` where order_id = ?";
//        update(sql,2);
//
        String sql = "update customers set name = ? where id = ?";

        update(sql,"张三",19);
    }
}
